package adapter;

import java.lang.reflect.Constructor;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.Inflater;

import com.example.tarefas.R;

import model.Tarefas;



import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TarefasAdapter extends BaseAdapter {

	protected Activity activity;
	protected Tarefas tarefas;
	private static LayoutInflater inflater=null;
	
	public TarefasAdapter(Activity activity, Tarefas tarefas)
	{
		this.activity = activity;
		this.tarefas = tarefas;
		
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	
	
	@Override
	public int getCount() {
		return this.tarefas.size();
	}

	@Override
	public Object getItem(int index) {
		return this.tarefas.get(index);
	}

	@Override
	public long getItemId(int index) {
		return this.tarefas.get(index).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View vi = convertView;
		if(convertView==null)
			vi = inflater.inflate(R.layout.item_lista,null);

		
		TextView descrText = (TextView)vi.findViewById(R.id.descrTxt);
		TextView horarioText = (TextView)vi.findViewById(R.id.horarioTxt);
		TextView idText = (TextView)vi.findViewById(R.id.idTxt);
		ImageView imgView = (ImageView)vi.findViewById(R.id.categoriaImg);
		
		descrText.setText(this.tarefas.get(position).getDescr());
		//horarioText.setText(this.tarefas.get(position).getHorario());
		idText.setText(this.tarefas.get(position).getStrId());
		
		Calendar calHorarioEvento = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
	    try {
			calHorarioEvento.setTime(sdf.parse(this.tarefas.get(position).getHorario()));//"Mon Mar 14 16:02:37 GMT 2011"
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// all done
		
	    /*
	    Calendar calHoje = Calendar.getInstance();
		int tempoDia = 1000 * 60 * 60 * 24;  
		long diasDiferenca = (calHoje.getTimeInMillis()-calHorarioEvento.getTimeInMillis());  
		horarioText.setText("Faltam " + diasDiferenca + " dias." + this.tarefas.get(position).getHorario());
		*/
	    
	    String horarioStr = "";
	    Timestamp horario = Timestamp.valueOf(this.tarefas.get(position).getHorario());
	    Timestamp horarioAgora = new Timestamp(System.currentTimeMillis());
	    
	    if (horario.before(horarioAgora))
	    {
	    	horarioStr = "Foi-se";
	    }
	    else
	    {
	    	long diferenca = horario.getTime()-horarioAgora.getTime();
	    	if(diferenca<3600000)
	    	{
	    		horarioStr = " A fazer em " + diferenca /(60*1000) + " mimutos";
	    	}
	    	else
	    	{
	    		horarioStr = " A fazer em " + diferenca / (3600000) + " horas";
	    	}
	    }
		
		horarioText.setText(horarioStr);	
		
		/* get Icon */
		int idCat = R.drawable.alerta;
		switch (this.tarefas.get(position).getCid()) {
		case 1:
			idCat = R.drawable.cat1;
			break;
		
		case 2:
			idCat = R.drawable.cat2;
			break;

		case 3:
			idCat = R.drawable.cat3;
			break;

		case 4:
			idCat = R.drawable.cat4;
			break;

		case 5:
			idCat = R.drawable.cat5;
			break;
		case 6:
			idCat = R.drawable.cat6;
			break;
		
		case 7:
			idCat = R.drawable.cat7;
			break;

		case 8:
			idCat = R.drawable.cat8;
			break;

		case 9:
			idCat = R.drawable.cat9;
			break;

		case 10:
			idCat = R.drawable.cat10;
			break;


			
			

		default:
			break;
		}
		
		imgView.setImageResource(idCat);
		
		
			
		
		
		
		
		
		return vi;
	}

}
